﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.Threading.Tasks;

namespace WJP.DataPortal.WCF
{
    /// <summary>
    /// 使用 WCF 实现的统一的数据门户。
    /// 标记了 ConcurrencyMode.Multiple 来表示多线程进行
    /// </summary>
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall, UseSynchronizationContext = false)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ServerPortal : IWcfPortal, IApiPortal
    {
        /// <summary>
        /// Fetch
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public WcfResponse Fetch(FetchRequest request)
        {
            //TODO:事件跟踪器

            DataPortalFacade dataPortalFacade = new DataPortalFacade();
            object obj;
            try
            {
                obj = dataPortalFacade.Fetch(request.ObjectType, request.Criteria, request.Context);
            }
            catch (Exception ex)
            {
                obj = ex;
            }
            
            return new WcfResponse() { Result = obj };
        }

        /// <summary>
        /// Update
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public WcfResponse Update(UpdateRequest request)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// API执行
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResponse Invoke(ApiRequest request)
        {
            throw new NotImplementedException();
        }
    }
}
